//
//  WebViewController.m
//  Hero
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "WebViewController.h"
#import "UMSocial.h"
#import "CollectManager.h"
#import "ConsultModel.h"
#import "YQLoginViewController.h"
@interface WebViewController ()<UMSocialUIDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, assign) BOOL isCollect;
@property (nonatomic, strong) UIBarButtonItem *item;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    if ([self.model.article_url hasPrefix:@"http"] == NO) {
        self.model.article_url = [NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@?APP_BROWSER_VERSION_CODE=1&android_version=1&imgmode=auto",self.model.article_url];
    }
    NSString *imageName = @"已收藏图片";
    self.isCollect = YES;
    if ([[CollectManager shareManager] isColllectWith:self.model.article_url] == NO) {
        imageName = @"未收藏图片";
        self.isCollect = NO;
    }

    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction)];
    [item setTag:4000];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"分享.png"] style:UIBarButtonItemStylePlain target:self action:@selector(shareAction)];
    self.item = item;
    self.navigationItem.rightBarButtonItems = @[item,item2];
    NSURL *url = [NSURL URLWithString:self.model.article_url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    [self.view addSubview:self.webView];
    
}
//收藏事件
- (void)collectionAction
{
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"auth"]) {
        if (self.isCollect) {
            [self.item setImage:[UIImage imageNamed:@"未收藏图片.png"]];
            [[CollectManager shareManager] removeCollectArticleWith:self.model];
            self.isCollect = NO;
        }else
        {
            self.item.image = [UIImage imageNamed:@"已收藏图片.png"];
            [[CollectManager shareManager] collectArticle:self.model];
            self.isCollect = YES;
        }
    }
    else {
        YQLoginViewController *loginVC = [YQLoginViewController new];
        UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        loginVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(barAction)];
        [self presentViewController:naVC animated:YES completion:nil];
    }
   
}
- (void)barAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//分享事件
- (void)shareAction
{
     [UMSocialSnsService presentSnsIconSheetView:self appKey:@"564913af67e58ed0d7005bb3" shareText:self.model.article_url shareImage:[UIImage imageNamed:@"meiyou.jpg"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToQQ,nil] delegate:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
